package com.dengyafei.controller;

import com.dengyafei.annotation.JwtToken;
import com.dengyafei.bean.EnterpriseInfoEntity;
import com.dengyafei.constant.RecommendConstant;
import com.dengyafei.constant.ResultMessage;
import com.dengyafei.domin.vo.EnterpriseInfoInsertVO;
import com.dengyafei.domin.vo.EnterpriseInfoSearchVO;
import com.dengyafei.domin.vo.EnterpriseInfoUpdateVO;
import com.dengyafei.result.RestApiResult;
import com.dengyafei.service.EnterpriseInfoService;
import com.dengyafei.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @author 邓亚非
 */
@RestController
@Slf4j
@RequestMapping("/enterpriseInfo")
public class EnterpriseInfoController {

    @Autowired
    EnterpriseInfoService enterpriseInfoService;

    /**
     * 企业入驻接口
     *
     * @param enterpriseInfoInsertVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/enterpriseInfoSettle")
    public RestApiResult enterpriseInfoSettle(@RequestBody EnterpriseInfoInsertVO enterpriseInfoInsertVO) {
        log.info("【企业模块-企业入驻接口-参数】enterpriseInfoInsertVO:[{}]", enterpriseInfoInsertVO);
        Integer integer = enterpriseInfoService.enterpriseInfoInsert(enterpriseInfoInsertVO);
        if (integer.equals(RecommendConstant.ENTERPRISE_SETTLED_FAILURE)) {
            log.error("【企业模块-企业入驻接口-失败】enterpriseInfoInsertVO,cause:[{}]", ResultMessage.EnterpriseInfo.ENTERPRISE_SETTLE_FAILURE);
            return RestApiResult.error(ResultMessage.EnterpriseInfo.ENTERPRISE_SETTLE_FAILURE);
        }
        log.info("【企业模块-企业入驻接口-成功】enterpriseInfoInsertVO:[{}]", ResultMessage.EnterpriseInfo.ENTERPRISE_SETTLE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.EnterpriseInfo.ENTERPRISE_SETTLE_SUCCESS);
    }

    /**
     * 查询公司信息
     *
     * @param
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryEnterpriseInfo")
    public RestApiResult queryEnterpriseInfo(HttpServletRequest request) {
        log.info("【企业模块-查询公司信息接口-参数】enterpriseQueryByNameVO:[{}]", "无参");
        String realToken = JwtUtil.getRealToken(request);
        String userInfo = JwtUtil.getUserInfo(realToken);
        EnterpriseInfoEntity enterpriseInfoEntity = enterpriseInfoService.queryByName(userInfo);
        log.info("【企业模块-查询公司信息接口-成功】enterpriseQueryByNameVO:[{}]", ResultMessage.EnterpriseInfo.QUERY_ENTERPRISE_INFO_SUCCESS);
        return RestApiResult.ok(enterpriseInfoEntity);
    }

    /**
     * 更新企业信息
     *
     * @param enterpriseInfoUpdateVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/updateEnterprise")
    public RestApiResult updateEnterprise(@RequestBody EnterpriseInfoUpdateVO enterpriseInfoUpdateVO) {
        log.info("【企业模块-修改企业信息接口-参数】enterpriseInfoUpdateVO:[{}]", enterpriseInfoUpdateVO);
        Integer integer = enterpriseInfoService.updateEnterpriseInfo(enterpriseInfoUpdateVO);
        if (integer.equals(RecommendConstant.ENTERPRISE_UPDATE_FAILURE)) {
            log.error("【企业模块-修改企业信息接口-失败】enterpriseInfoUpdateVO,cause:[{}]", ResultMessage.EnterpriseInfo.ENTERPRISE_INFO_UPDATE_FAILURE);
            return RestApiResult.error(ResultMessage.EnterpriseInfo.ENTERPRISE_INFO_UPDATE_FAILURE);
        }
        log.info("【企业模块-修改企业信息接口-成功】enterpriseInfoUpdateVO:[{}]", ResultMessage.EnterpriseInfo.ENTERPRISE_INFO_UPDATE_SUCCESS);
        return RestApiResult.okMsg(ResultMessage.EnterpriseInfo.ENTERPRISE_INFO_UPDATE_SUCCESS);
    }

    /**
     * 查询所有的公司信息
     * @return
     */
    @JwtToken
    @PostMapping(value = "/queryAllEnterpriseInfo")
    public RestApiResult queryAllEnterpriseInfo(){
        log.info("【企业模块-查询所有的企业接口-参数】:[{}]","无参");
        List<EnterpriseInfoEntity> enterpriseInfoEntities = enterpriseInfoService.queryAll();
        log.info("【企业模块-查询所有得企业接口-成功】:[{}]",ResultMessage.EnterpriseInfo.QUERY_ENTERPRISE_INFO_SUCCESS);
        return RestApiResult.ok(enterpriseInfoEntities);
    }

    /**
     * 条件查询
     * @param enterpriseInfoSearchVO
     * @return
     */
    @JwtToken
    @PostMapping(value = "/search")
    public RestApiResult search(@RequestBody EnterpriseInfoSearchVO enterpriseInfoSearchVO){
        log.info("【企业模块-搜索接口-参数】enterpriseInfoSearchVO:[{}]",enterpriseInfoSearchVO);
        List<EnterpriseInfoEntity> enterpriseInfoEntities = enterpriseInfoService.queryByParams(enterpriseInfoSearchVO);
        log.info("【企业模块-搜索接口-成功】enterpriseInfoSearchVO:[{}]",ResultMessage.EnterpriseInfo.QUERY_ENTERPRISE_INFO_SUCCESS);
        return RestApiResult.ok(enterpriseInfoEntities);
    }

    /**
     * 企业信息
     * @return
     */
    @PostMapping(value = "/queryEnterpriseInfos")
    public RestApiResult queryEnterpriseInfos(){
        List<EnterpriseInfoEntity> enterpriseInfoEntities = enterpriseInfoService.queryEnterpriseInfos();
        return RestApiResult.ok(enterpriseInfoEntities);
    }
}